package com.paishen.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;
import com.paishen.common.R;
/**
 * Created by peng on 2015/12/4.
 */
public class SelectionDialog extends Dialog {

    public SelectionDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Menu {
        View.OnClickListener listener;
        String funName;

        public Menu(String funName, View.OnClickListener listener) {
            this.funName = funName;
            this.listener = listener;
        }
    }

    public static class Builder {
        private final Context context;
        private final boolean shadow = true;
        private final boolean canCancel = true;

        private final List<Menu> menuList = new ArrayList<>();

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMenuList(List<Menu> data) {
            if (data != null) menuList.addAll(data);
            return this;
        }

        public SelectionDialog create() {
            final SelectionDialog dialog = new SelectionDialog(context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);

            LinearLayout view = new LinearLayout(context);
            view.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams lpItem = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams lpDevider = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
            int dpValue = 8;
            int spacing = (int) (dpValue * context.getResources().getDisplayMetrics().density + 0.5f);

            for (int i = 0; i < menuList.size(); i++) {
                Menu bottomMenu = menuList.get(i);
                Button bbm = new Button(context);
                bbm.setLayoutParams(lpItem);
                int backgroundResId = R.drawable.common_dialog_selection_selector_singleton;
                if (menuList.size() > 1) {
                    if (i == 0) {
                        backgroundResId = R.drawable.common_dialog_selection_selector_top;
                    } else if (i == menuList.size() - 1) {
                        backgroundResId = R.drawable.common_dialog_selection_selector_bottom;
                    } else {
                        backgroundResId = R.drawable.common_dialog_selection_selector_center;
                    }
                }
                bbm.setBackgroundResource(backgroundResId);
                bbm.setPadding(spacing * 2, (int) (spacing * 1.5f), spacing * 2, (int) (spacing * 1.5f));
                bbm.setGravity(Gravity.CENTER);
                bbm.setText(bottomMenu.funName);
                bbm.setTextColor(0xFF05131A);
                bbm.setTextSize(15);
                bbm.setOnClickListener(bottomMenu.listener);
                view.addView(bbm);

                if (i != menuList.size() - 1) {
                    View viewDivider = new View(context);
                    viewDivider.setLayoutParams(lpDevider);
                    viewDivider.setBackgroundColor(0xFFB3B3B3);
                    view.addView(viewDivider);
                }
            }

            Window win = dialog.getWindow();
            win.getDecorView().setPadding(spacing * 3, 0, spacing * 3, 0);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);

            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(canCancel);
            dialog.setCancelable(canCancel);

            return dialog;
        }
    }
}
